package com.gcsj.gcsjspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcsj.gcsjspringboot.common.utils.PageUtils;
import com.gcsj.gcsjspringboot.common.utils.Query;
import com.gcsj.gcsjspringboot.common.utils.R;
import com.gcsj.gcsjspringboot.entity.SysVar;
import com.gcsj.gcsjspringboot.service.SysVarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/var")
public class SysValController {
    @Autowired
    private SysVarService varService;

    @PostMapping("/page")
    public R page(@RequestBody Map<String, Object> params){
        String varName = (String)params.get("varName");
        IPage<SysVar> page = null;
        if(StringUtils.isEmpty(varName)){
            page = varService.page(new Query<SysVar>().getPage(params), null);
        } else {
            page = varService.page(new Query<SysVar>().getPage(params), new QueryWrapper<SysVar>().like("sys_name", varName));
        }
        return R.ok().put("page", new PageUtils(page));
    }

    @PostMapping("/insert")
    public R insert(@RequestBody SysVar var){
        boolean save = varService.save(var);
        if(save){
            return R.ok();
        }
        return R.error();
    }

    @PostMapping("/update")
    public R update(@RequestBody SysVar var){
        boolean ret = varService.updateById(var);
        if(ret){
            return R.ok();
        }
        return R.error();
    }
    @PostMapping("/del")
    public R del(Integer id){
        boolean b = varService.removeById(id);
        if(b){
            return R.ok();
        }
        return R.error();
    }
}
